package com.where_to_play;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.example.where_to_play.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.where_to_play.util.FragmentChangeHelper;
import com.where_to_play_search.Search_Fragment;

public class MainActivity extends FragmentActivity {
	private MainActivity mactivity = this;
	private FragmentManager supportFragmentManager;
	@ViewInject(R.id.layout_container_fragment)
	private LinearLayout layout_container_fragment;
	@ViewInject(R.id.radioGroup_main)
	private RadioGroup radioGroup_main;
	private Fragment fragment = null;
	private FragmentChangeHelper helper;
	private Search_Fragment search_fragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getActionBar().hide();
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		supportFragmentManager = getSupportFragmentManager();
		ViewUtils.inject(mactivity);

		radioGroup_main
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						switch (checkedId) {
						case R.id.radioButton_guide_home:
							Toast.makeText(mactivity, "首页", 1).show();
							break;
						case R.id.radioButton_guide_search:
							if (search_fragment == null) {
								search_fragment = new Search_Fragment();
							}
							fragment = search_fragment;

							Toast.makeText(mactivity, "发现", 1).show();
							break;
						case R.id.radioButton_guide_service:
							Toast.makeText(mactivity, "客服", 1).show();
							break;
						case R.id.radioButton_guide_center:
							Toast.makeText(mactivity, "我的", 1).show();
							break;

						default:

							break;
						}

						helper = new FragmentChangeHelper(fragment);
						helper.clearAllBackStack(true);
						changeFragment(helper);
					}
				});
	}

	// 自定义碎片统一管理方法
	public void changeFragment(FragmentChangeHelper helper) {

		if (helper != null) {
			FragmentTransaction tran = supportFragmentManager
					.beginTransaction();

			Fragment targetFragment = helper.getTargetFragment();

			if (targetFragment != null) {

				// 检查是否附带参数
				Bundle args = helper.getArgs();
				if (args != null) {
					targetFragment.setArguments(args);
				}

				// 检查是否添加到回退栈
				String targetFragmentTag = helper.getTargetFragmentTag();
				if (targetFragmentTag != null) {
					tran.addToBackStack(targetFragmentTag);
				}

				// 检查是否要删除回退栈里对应的Fragment
				String[] removeFragmentTag = helper.getRemoveFragmentTag();
				if (removeFragmentTag != null) {
					for (String s : removeFragmentTag) {
						tran.remove(supportFragmentManager.findFragmentByTag(s));
					}
				}

				// 检查是否清空回退栈
				if (helper.isClearAllBackStack()) {
					supportFragmentManager.popBackStack(null,
							FragmentManager.POP_BACK_STACK_INCLUSIVE);
				}

				tran.replace(R.id.layout_container_fragment, targetFragment);

				tran.commit();

			} else { // 如果没有目标Fragment，代表需要返回到栈中的指定Fragment
				String backToFragmentTag = helper.getBackToFragmentTag();

				if (backToFragmentTag != null) {
					supportFragmentManager.popBackStackImmediate(
							backToFragmentTag,
							FragmentManager.POP_BACK_STACK_INCLUSIVE);
				}
			}
		}
	}
}
